#ifndef FAT16_H
#define FAT16_H

#include "../../tools/headers/common.h"



//File attribute values
#define READ_ONLY		0x01
#define HIDDEN			0x02
#define SYSTEM			0x04
#define VOLUME_LABEL	0x08
#define DIRECTORY		0x10
#define ARCHIVED		0x20

#define DELETED			0xE5

//---Structures---//
struct BootBlock
{
	u8int description[9];		//0x03  8 chars plus zero
	u16int bytesPerBlock;		//0x0b  
	u8int blocksPerAllocUnit;	//0x0d
	u16int reservedBlocks;		//0x0e
	u8int numberOfFats;		//0x10
	u16int rootDirEntriesN;		//0x11
	u32int totalBlocks;			//0x13 or 0x20 (if 0x13 == 0)
	u8int mediaDescriptor;		//0x15
	u16int blocksPerFat;	//0x16 /One FAT size
	u32int volumeSerialNumber;	//0x27
	u8int volumeLabel[5];		//0x2b  4 chars plus zero
	u8int fileSystemId[9];		//0x36	8 chars plus zero
	//--------Calculated fields----------------//
	u32int rootDirStartSector; //Starting from the first sector of partition (not drive)
	u16int rootDirSize;
};
typedef struct BootBlock BootBlock;

/**
	Represent directory (or file) table.
	@todo Convert time to readable format.
*/
struct FileDescriptor
{
	s8int name[9];
	s8int extension[4];
	BOOL isReadOnly;
	BOOL isHidden;
	BOOL isSystem;
	BOOL isVolLabel;
	BOOL isDirectory;
	u16int timeLastUpdated;
	u16int dateLastUpdated;
	u16int startingCluster;
	u32int size;
	s8int partition;
};
typedef struct FileDescriptor FileDescriptor;
//------------------------//

BOOL isDir(u8int* entry);
BOOL isDeleted(u8int* entry);
BOOL testAttribute(u8int* entry, u8int test);
u32int getDirectoryFromPath(s8int* path, FileDescriptor* resFileInfo, BOOL ignoreVolLabel);
void fillFileDescriptor(u8int* entry, FileDescriptor* file, s8int partition);
void getFileList(s8int *path, s8int *res);
u32int clusterToSector(u32int cluster, BootBlock *bootBlock);
s32int findEntry(u8int *buff,
				s32int numberOfEntries,
				s8int *name,
				s8int nameLngt,
				s8int *extension,
				s8int extLngt,
				BOOL ignoreVolLabel);
u32int loadFile(FileDescriptor* file, u8int* buff);



#endif